package com.distribuida.supertrunfo.impl;

import java.util.ArrayList;
import java.util.List;

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;

public class SuperTrunfo {

	private String nome;
	private ArrayList<Carta> cartas = new ArrayList<Carta>();
	
	public SuperTrunfo(String arquivoXML){
		Document doc = null;
        SAXBuilder builder = new SAXBuilder();
        try {
        	doc = builder.build(arquivoXML);
        } catch (Exception e) {
        	e.printStackTrace();
        }           
        Element superTrunfoElement = doc.getRootElement();
        this.nome = superTrunfoElement.getAttributeValue("nome");
        List<Element> elements = superTrunfoElement.getChildren();
        ArrayList<Caracteristica> caracteristicas = new ArrayList<Caracteristica>();
        for (Element element: elements) {
        	if (element.getName().equals("caracteristicas")) {
        		List<Element> caracteristicasElements = element.getChildren();
        		for (Element caracteristicaElement: caracteristicasElements) {
        			System.out.println("\n---------Criando Caracteristica---------");
        			System.out.println("ID: " + caracteristicaElement.getAttributeValue("id"));
        			System.out.println("Tipo: " + caracteristicaElement.getAttributeValue("tipo"));
        			System.out.println("Nome: " + caracteristicaElement.getContent().get(0).getValue());
        			caracteristicas.add(new Caracteristica(caracteristicaElement.getAttributeValue("id"), 
        												   caracteristicaElement.getAttributeValue("tipo"),
        												   caracteristicaElement.getContent().get(0).getValue()));
        		}
        	} else if (element.getName().equals("cartas")) {
        		List<Element> cartasElements = element.getChildren();
        		for (Element cartaElement: cartasElements) {
        			System.out.println("\n---------Criando Carta---------");
        			Carta carta = new Carta(cartaElement.getAttributeValue("codigo"), cartaElement.getAttributeValue("trunfo"), cartaElement.getAttributeValue("vencetrunfo"));
        			carta.setCaracteristicas(caracteristicas);
        			List<Element> valoresElements = cartaElement.getChildren();
        			for (Element valorElement: valoresElements) {
        				carta.setValorCaracteristicas(valorElement.getAttributeValue("caracteristica"), 
        											  valorElement.getContent().get(0).getValue());
        			}
        			this.getCartas().add(carta);
        			System.out.println(carta.toString());
        		}
        	}
        }	
	}

	public String getNome() {
		return nome;
	}
	public void setNome(String nome) {
		this.nome = nome;
	}
	
	public ArrayList<Carta> getCartas() {
		return cartas;
	}
	public void setCartas(ArrayList<Carta> cartas) {
		this.cartas = cartas;
	}
	
    public Carta getCartaByID(String codigo){
        for(Carta carta : cartas){
            if(carta.getCodigo().equalsIgnoreCase(codigo)){
                return carta;
            }
        }
        return null;
    }


}
